//
//  MinButtonViewBtn.m
//  XinLiJianKangJiaoYu
//
//  Created by 李文强 on 2018/12/6.
//  Copyright © 2018 sijiaokeji. All rights reserved.
//

#import "MinButtonViewBtn.h"

@implementation MinButtonViewBtn

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//- (instancetype)initWithFrame:(CGRect)frame{
//
//}
- (instancetype)initWithFrame:(CGRect)frame andType:(ButtonImageType)type{
    if ([super initWithFrame:frame]) {
        self.myButton = type;
        [self initSubView];
        [self initOption];
        [self layout];
    }
    return self;
}
- (void)initSubView{
    [self addSubview:self.topImage];
    [self addSubview:self.bottomLabel];
}
- (void)initOption{
    [self setTitle:@" " forState:UIControlStateNormal];
    
}
- (void)layout{
    CGFloat W = [PublickFunctionObject getLabelWith:_bottomLabel].width+10;
    if (self.myButton == ButtonImageInLeft) {
        [_bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.centerX.equalTo(self.mas_centerX).offset(10);
            make.top.equalTo(@0);
            make.bottom.equalTo(@0);
            make.right.equalTo(self.mas_right).offset(-10);
        }];
        [_topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@15);
            make.right.equalTo(self.bottomLabel.mas_left).offset(-10);
            make.height.equalTo(@15);
        }];
      
    }else if (self.myButton == ButtonImageInRight){
        CGFloat W = [PublickFunctionObject getLabelWith:_bottomLabel].width;
        [_bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.right.equalTo(@0).offset(-20);
            make.top.equalTo(@0);
            make.bottom.equalTo(@0);
            make.width.equalTo(@0).offset(W);
        }];
        [_topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bottomLabel.mas_centerY);
            make.width.equalTo(@15);
            make.right.equalTo(@0).offset(0);
            make.height.equalTo(@10);
        }];
    }else if (self.myButton == ButtonImageInBootom){
        [_topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.top.equalTo(self.topImage.mas_bottom);
            make.bottom.equalTo(@0);
        }];
        [_bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.width.equalTo(@30);
            make.bottom.equalTo(self.mas_centerY).offset(5);
            make.height.equalTo(@30);
        }];
    }else if(self.myButton == ButtonImageInSelectedBtn){
        [_bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.centerX.equalTo(self.mas_centerX);
            make.top.equalTo(@0);
            make.bottom.equalTo(@0);
            make.width.equalTo(@0).offset(W);
        }];
        [_topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bottomLabel.mas_centerY);
            make.width.equalTo(@10);
            make.left.equalTo(self.bottomLabel.mas_right).offset(0);
            make.height.equalTo(@7);
        }];
    }else{
     
        [_topImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.width.equalTo(@23);
            make.bottom.equalTo(self.mas_centerY).offset(5);
            make.height.equalTo(@22);
        }];
        [_bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.top.equalTo(self.topImage.mas_bottom).offset(0);
            make.bottom.equalTo(@0);
        }];
    }

}
- (void)setLayout{
    if (self.myButton == ButtonImageInRight){
        CGFloat W = [PublickFunctionObject getLabelWith:_bottomLabel].width+10;
        [_bottomLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.left.equalTo(@0);
            make.top.equalTo(@0);
            make.bottom.equalTo(@0);
            make.width.equalTo(@0).offset(W);
        }];
        [_topImage mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bottomLabel.mas_centerY);
            make.width.equalTo(@15);
            make.left.equalTo(self.bottomLabel.mas_right).offset(0);
            make.height.equalTo(@10);
        }];
    }
}

- (UIImageView *)topImage{
    if (!_topImage) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.backgroundColor = [UIColor clearColor];
        _topImage = imageView;
    }
    return _topImage;
}
- (UILabel *)bottomLabel{
    if (!_bottomLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor darkGrayColor];
        label.textAlignment = 1;
        label.text = @"";
        _bottomLabel = label;
    }
    return _bottomLabel;
}
@end
